def getValueFromArray(arr, index, default=''):
    if index >= len(arr):
        print('miss value')
        return default
    return arr[index]


def findOneElementByXPath(ele, xpath):
    eles = ele.find_elements_by_xpath(xpath)
    if len(eles) == 0:
        return None
    return eles[0]


def rule_parse(webelement, rules, result):
    if webelement is None or rules is None:
        print('miss webelement')
        return
    for rule in rules:
        if rule['type'] == 'element':
            ele = findOneElementByXPath(webelement, rule['xpath'])
            rule_parse(ele, rule['nodes'], result)
        elif rule['type'] == 'attr':
            value = webelement.get_attribute(rule['attr_key'])
            split = rule.get('split')
            if split is None:
                result[rule['col_key']] = value
            else:
                values = value.split(split['split_text'])
                for k in split['split_rules']:
                    result[k['col_key']] = getValueFromArray(values, k['position'])
                if rule.get('col_key') is not None:
                    result[rule['col_key']] = value